<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="教程,">










<meta name="description" content="蜂鸣器从结构区分分为压电式蜂鸣器和电磁式蜂鸣器。压电式为压电陶瓷片发音，电流比较小一些，电磁式蜂鸣器为线圈通电震动发音，体积比较小。 按照驱动方式分为有源蜂鸣器和无源蜂鸣器。这里的有源和无源不是指电源，而是振荡源。有源蜂鸣器内部带了振荡源，如图 9-8 所示中，给了 BUZZ 引脚一个低电平，蜂鸣器就会直接响。而无源蜂鸣器内部是不带振荡源的，要让他响必须给 500Hz～4.5KHz 之间的脉冲频率">
<meta name="keywords" content="教程">
<meta property="og:type" content="article">
<meta property="og:title" content="9.9、电机控制程序基础">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2014/12/01/mcu064/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="蜂鸣器从结构区分分为压电式蜂鸣器和电磁式蜂鸣器。压电式为压电陶瓷片发音，电流比较小一些，电磁式蜂鸣器为线圈通电震动发音，体积比较小。 按照驱动方式分为有源蜂鸣器和无源蜂鸣器。这里的有源和无源不是指电源，而是振荡源。有源蜂鸣器内部带了振荡源，如图 9-8 所示中，给了 BUZZ 引脚一个低电平，蜂鸣器就会直接响。而无源蜂鸣器内部是不带振荡源的，要让他响必须给 500Hz～4.5KHz 之间的脉冲频率">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://c.biancheng.net/cpp/uploads/allimg/140318/1-14031Q15134296.png">
<meta property="og:updated_time" content="2019-01-27T04:56:58.615Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="9.9、电机控制程序基础">
<meta name="twitter:description" content="蜂鸣器从结构区分分为压电式蜂鸣器和电磁式蜂鸣器。压电式为压电陶瓷片发音，电流比较小一些，电磁式蜂鸣器为线圈通电震动发音，体积比较小。 按照驱动方式分为有源蜂鸣器和无源蜂鸣器。这里的有源和无源不是指电源，而是振荡源。有源蜂鸣器内部带了振荡源，如图 9-8 所示中，给了 BUZZ 引脚一个低电平，蜂鸣器就会直接响。而无源蜂鸣器内部是不带振荡源的，要让他响必须给 500Hz～4.5KHz 之间的脉冲频率">
<meta name="twitter:image" content="http://c.biancheng.net/cpp/uploads/allimg/140318/1-14031Q15134296.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2014/12/01/mcu064/">





  <title>9.9、电机控制程序基础 | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2014/12/01/mcu064/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">9.9、电机控制程序基础</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2014-12-01T20:36:47+08:00">
                2014-12-01
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/单片机教程/" itemprop="url" rel="index">
                    <span itemprop="name">单片机教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>蜂鸣器从结构区分分为压电式蜂鸣器和电磁式蜂鸣器。压电式为压电陶瓷片发音，电流比较小一些，电磁式蜂鸣器为线圈通电震动发音，体积比较小。</p>
<p>按照驱动方式分为有源蜂鸣器和无源蜂鸣器。这里的有源和无源不是指电源，而是振荡源。有源蜂鸣器内部带了振荡源，如图 9-8 所示中，给了 BUZZ 引脚一个低电平，蜂鸣器就会直接响。而无源蜂鸣器内部是不带振荡源的，要让他响必须给 500Hz～4.5KHz 之间的脉冲频率信号来驱动它才会响。有源蜂鸣器往往比无源蜂鸣器贵一些，因为里边多了振荡电路，驱动发音也简单，靠电平就可以驱动，而无源蜂鸣器价格比较便宜，此外无源蜂鸣器声音频率可以控制，而音阶与频率又有确定的对应关系，因此就可以做出来“do re mi fa sol la si”的效果，可以用它制作出简单的音乐曲目，比如生日歌、两只老虎等等。</p>
<p><img src="http://c.biancheng.net/cpp/uploads/allimg/140318/1-14031Q15134296.png" alt></p>
<center><strong>图 9-8  蜂鸣器电路原理图</strong></center>

<p>我们来看一下图 9-8 的电路，蜂鸣器电流依然相对较大，因此需要用三极管驱动，并且加了一个 100 欧的电阻作为限流电阻。此外还加了一个 D4 二极管，这个二极管叫做续流二极管。我们的蜂鸣器是感性器件，当三极管导通给蜂鸣器供电时，就会有导通电流流过蜂鸣器。而我们知道，电感的一个特点就是电流不能突变，导通时电流是逐渐加大的，这点没有问题，但当关断时，经“电源-三极管-蜂鸣器-地”这条回路就截断了，过不了任何电流了，那么储存的电流往哪儿去呢，就是经过这个 D4 和蜂鸣器自身的环路来消耗掉了，从而就避免了关断时由于电感电流造成的反向冲击。接续关断时的电流，这就是续流二极管名称的由来。</p>
<p>蜂鸣器经常用于电脑、打印机、万用表这些设备上做提示音，提示音一般也很简单，就是简单发出个声音就行，我们用程序简单做了个 4KHZ 频率下的发声和 1KHZ 频率下的发声程序，同学们可以自己研究下程序，比较下实际效果。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">sbit BUZZ = P1^6; //蜂鸣器控制引脚</span><br><span class="line">unsigned char T0RH = 0; //T0 重载值的高字节</span><br><span class="line">unsigned char T0RL = 0; //T0 重载值的低字节</span><br><span class="line">void OpenBuzz(unsigned int frequ);</span><br><span class="line">void StopBuzz();</span><br><span class="line">void main()&#123;</span><br><span class="line">    unsigned int i;</span><br><span class="line">    TMOD = 0x01; //配置 T0 工作在模式 1，但先不启动</span><br><span class="line">    EA = 1;</span><br><span class="line">    while (1)&#123; //使能全局中断</span><br><span class="line">        OpenBuzz(4000); //以 4KHz 的频率启动蜂鸣器</span><br><span class="line">        for (i=0; i&lt;40000; i++);</span><br><span class="line">        StopBuzz(); //停止蜂鸣器</span><br><span class="line">        for (i=0; i&lt;40000; i++);</span><br><span class="line">        OpenBuzz(1000); //以 1KHz 的频率启动蜂鸣器</span><br><span class="line">        for (i=0; i&lt;40000; i++);</span><br><span class="line">        StopBuzz(); //停止蜂鸣器</span><br><span class="line">        for (i=0; i&lt;40000; i++);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 蜂鸣器启动函数，frequ-工作频率 */</span><br><span class="line">void OpenBuzz(unsigned int frequ)&#123;</span><br><span class="line">    unsigned int reload;//计算所需的定时器重载值</span><br><span class="line">    reload = 65536 - (11059200/12)/(frequ*2); //由给定频率计算定时器重载值</span><br><span class="line">    T0RH = (unsigned char)(reload &gt;&gt; 8); //16 位重载值分解为高低两个字节</span><br><span class="line">    T0RL = (unsigned char)reload;</span><br><span class="line">    TH0 = 0xFF; //设定一个接近溢出的初值，以使定时器马上投入工作</span><br><span class="line">    TL0 = 0xFE;</span><br><span class="line">    ET0 = 1; //使能 T0 中断</span><br><span class="line">    TR0 = 1; //启动 T0</span><br><span class="line">&#125;</span><br><span class="line">/* 蜂鸣器停止函数 */</span><br><span class="line">void StopBuzz()&#123;</span><br><span class="line">    ET0 = 0; //禁用 T0 中断</span><br><span class="line">    TR0 = 0; //停止 T0</span><br><span class="line">&#125;</span><br><span class="line">/* T0 中断服务函数，用于控制蜂鸣器发声 */</span><br><span class="line">void InterruptTimer0() interrupt 1&#123;</span><br><span class="line">    TH0 = T0RH; //重新加载重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    BUZZ = ~BUZZ; //反转蜂鸣器控制电平</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>另外用蜂鸣器来输出音乐，仅仅是好玩而已，应用很少，里边包含了音阶、乐谱的相关内容，程序也有一点复杂，所以就不详细给大家去讲解了。仅提供一个可以播放《两只老虎》的程序，大家可以下载到板子上玩玩，满足一下好奇心。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">sbit BUZZ = P1^6; //蜂鸣器控制引脚</span><br><span class="line">unsigned int code NoteFrequ[] = &#123; //中音 1-7 和高音 1-7 对应频率列表</span><br><span class="line">    523, 587, 659, 698, 784, 880, 988, //中音 1-7</span><br><span class="line">    1047, 1175, 1319, 1397, 1568, 1760, 1976 //高音 1-7</span><br><span class="line">&#125;;</span><br><span class="line">unsigned int code NoteReload[] = &#123; //中音 1-7 和高音 1-7 对应的定时器重载值</span><br><span class="line">    65536 - (11059200/12) / (523*2), //中音 1</span><br><span class="line">    65536 - (11059200/12) / (587*2), //2</span><br><span class="line">    65536 - (11059200/12) / (659*2), //3</span><br><span class="line">    65536 - (11059200/12) / (698*2), //4</span><br><span class="line">    65536 - (11059200/12) / (784*2), //5</span><br><span class="line">    65536 - (11059200/12) / (880*2), //6</span><br><span class="line">    65536 - (11059200/12) / (988*2), //7</span><br><span class="line">    65536 - (11059200/12) / (1047*2), //高音 1</span><br><span class="line">    65536 - (11059200/12) / (1175*2), //2</span><br><span class="line">    65536 - (11059200/12) / (1319*2), //3</span><br><span class="line">    65536 - (11059200/12) / (1397*2), //4</span><br><span class="line">    65536 - (11059200/12) / (1568*2), //5</span><br><span class="line">    65536 - (11059200/12) / (1760*2), //6</span><br><span class="line">    65536 - (11059200/12) / (1976*2), //7</span><br><span class="line">&#125;;</span><br><span class="line">bit enable = 1; //蜂鸣器发声使能标志</span><br><span class="line">bit tmrflag = 0; //定时器中断完成标志</span><br><span class="line">unsigned char T0RH = 0xFF; //T0 重载值的高字节</span><br><span class="line">unsigned char T0RL = 0x00; //T0 重载值的低字节</span><br><span class="line">void PlayTwoTiger();</span><br><span class="line">void main()&#123;</span><br><span class="line">    unsigned int i;</span><br><span class="line">    EA = 1; //使能全局中断</span><br><span class="line">    TMOD = 0x01; //配置 T0 工作在模式 1</span><br><span class="line">    TH0 = T0RH;</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    ET0 = 1; //使能 T0 中断</span><br><span class="line">    TR0 = 1; //启动 T0</span><br><span class="line">   </span><br><span class="line">    while (1)&#123;</span><br><span class="line">        PlayTwoTiger(); //播放乐曲--两支老虎</span><br><span class="line">        for (i=0; i&lt;40000; i++); //停止一段时间</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 两支老虎乐曲播放函数 */</span><br><span class="line">void PlayTwoTiger()&#123;</span><br><span class="line">    unsigned char beat; //当前节拍索引</span><br><span class="line">    unsigned char note; //当前节拍对应的音符</span><br><span class="line">    unsigned int time = 0; //当前节拍计时</span><br><span class="line">    unsigned int beatTime = 0; //当前节拍总时间</span><br><span class="line">    unsigned int soundTime = 0; //当前节拍需发声时间</span><br><span class="line">    //两只老虎音符表</span><br><span class="line">    unsigned char code TwoTigerNote[] = &#123;</span><br><span class="line">    1,   2,   3, 1,    1,   2,   3, 1,     3, 4, 5,   3, 4, 5,</span><br><span class="line">    5,6, 5,4, 3, 1,    5,6, 5,4, 3, 1,     1, 5, 1,   1, 5, 1,</span><br><span class="line">   </span><br><span class="line">    &#125;;</span><br><span class="line">    //两只老虎节拍表，4 表示一拍，1 就是 1/4 拍，8 就是 2 拍</span><br><span class="line">    unsigned char code TwoTigerBeat[] = &#123;</span><br><span class="line">    4,   4,   4, 4,   4,   4,   4, 4,    4, 4, 8,   4, 4, 8,</span><br><span class="line">    3,1, 3,1, 4, 4,   3,1, 3,1, 4, 4,    4, 4, 8,   4, 4, 8,</span><br><span class="line">    &#125;;</span><br><span class="line">   </span><br><span class="line">    //用节拍索引作为循环变量</span><br><span class="line">    for (beat=0; beat&lt;sizeof(TwoTigerNote); )&#123;</span><br><span class="line">        while (!tmrflag); //每次定时器中断完成后，检测并处理节拍</span><br><span class="line">        tmrflag = 0;</span><br><span class="line">        if (time == 0)&#123; //当前节拍播完则启动一个新节拍</span><br><span class="line">            note = TwoTigerNote[beat] - 1;</span><br><span class="line">            T0RH = NoteReload[note] &gt;&gt; 8;</span><br><span class="line">            T0RL = NoteReload[note];</span><br><span class="line">            //计算节拍总时间，右移 2 位相当于除 4，移位代替除法可以加快执行速度</span><br><span class="line">            beatTime = (TwoTigerBeat[beat] * NoteFrequ[note]) &gt;&gt; 2;</span><br><span class="line">            //计算发声时间，为总时间的 0.75，移位原理同上</span><br><span class="line">            soundTime = beatTime - (beatTime &gt;&gt; 2);</span><br><span class="line">            enable = 1; //指示蜂鸣器开始发声</span><br><span class="line">            time++;</span><br><span class="line">        &#125;else&#123; //当前节拍未播完则处理当前节拍</span><br><span class="line">            //当前持续时间到达节拍总时间时归零，</span><br><span class="line">            //并递增节拍索引，以准备启动新节拍</span><br><span class="line">            if (time &gt;= beatTime)&#123;</span><br><span class="line">                time = 0;</span><br><span class="line">                beat++;</span><br><span class="line">            &#125;else&#123; //当前持续时间未达到总时间时，</span><br><span class="line">                time++; //累加时间计数</span><br><span class="line">                //到达发声时间后，指示关闭蜂鸣器，</span><br><span class="line">                //插入 0.25*总时间的静音间隔，</span><br><span class="line">                if (time == soundTime)&#123;</span><br><span class="line">                    enable = 0; //用以区分连续的两个节拍</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* T0 中断服务函数，用于控制蜂鸣器发声 */</span><br><span class="line">void InterruptTimer0() interrupt 1&#123;</span><br><span class="line">    TH0 = T0RH; //重新加载重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    tmrflag = 1;</span><br><span class="line">    if (enable)&#123; //使能时反转蜂鸣器控制电平</span><br><span class="line">        BUZZ = ~BUZZ;</span><br><span class="line">    &#125;else&#123; //未使能时关闭蜂鸣器</span><br><span class="line">        BUZZ = 1;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/教程/" rel="tag"># 教程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2014/12/01/mcu063/" rel="next" title="9.8、电机控制程序基础">
                <i class="fa fa-chevron-left"></i> 9.8、电机控制程序基础
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/limh/2014/12/01/mcu065/" rel="prev" title="10.1、单片机数字秒表程序">
                10.1、单片机数字秒表程序 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
